<?php
namespace app\admin\model;

class Uyongjin extends BaseModel
{
    function yongjinduihuan($hand_id,$user_id=null){
        $where['y.is_fa']=0;
        if ($user_id!=null){
            $where['y.user_id']=$user_id;
        }
    
        $user = D('user');
        //查询一个用户未结算的佣金
        $list = $user->alias('u')
                     ->field('y.user_id,u.openid,u.nickname,u.imgurl,sum(y.money) as money,y.create_time')
                     ->join('__UYONGJIN__ y','u.id=y.user_id')
                     ->where($where)
                     ->order('y.create_time','desc')
                     ->group('y.user_id')
                     ->select();
        $duihuan = D("duihuan");
        foreach ($list as $tmp){ 
            $duihuanData['user_id'] = $tmp['user_id'];
            $duihuanData['hand_id'] = $hand_id;
            $duihuanData['money'] = $tmp['money'];
            $duihuanData['type'] = 0;
            $duihuan->insert($duihuanData);
        }
        
        
        
    }
}